import typing
from provate import list2pList, listNode, print_List_node

# 题目：定义一个函数，输入一个链表的头节点反转该链表并输出反转后链表的头节点

def reverse_list(head:listNode)->(listNode or None):
    if not head or head.next is None:
        return head

    reverse_head = None
    p_node = head
    p_pre = None

    while p_node is not None:
        p_next = p_node.next

        if p_next is None:
            reverse_head = p_node

        p_node.next = p_pre

        p_pre = p_node
        p_node = p_next

    return reverse_head


if __name__ == '__main__':
    l1 = [1,2,3,4,2,5,1,9]
    head = list2pList(l1)
    print_List_node(head)
    reverse_head = reverse_list(head)
    print_List_node(reverse_head)

